package com.qiankunxu.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * @author : Hunter Huang
 * @since : 2023/12/16 11:05
 * {@literal : some description}
 */

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    // 当Spring容器加载完BeanDefinition之后，会调用BeanFactoryPostProcessor的postProcessBeanFactory方法

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        //注册一个Teacher的BeanDefinition
        GenericBeanDefinition gbdf = new GenericBeanDefinition();
        gbdf.setBeanClass(Teacher.class);
        //向下强转
        DefaultListableBeanFactory bdf = (DefaultListableBeanFactory) beanFactory;

        bdf.registerBeanDefinition("teacher",gbdf);

        System.out.println("MyBeanFactoryPostProcessor");
    }
}
